package com.inossem.warehousestorage_inossem.adapter.inventory.chuangyou;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.chuangyou.InventoryMatDetailsActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.extre.InventoryExtre;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.utils.EditTextWatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/2 10:48
 * @email jianyu.zhan@inossem.com
 */
public class InventoryMatsAdapter extends BaseAdapter {

    private Context context;
    private List<ItemListBean> list;
    private String stocktake_type;
    private String location_id;
    private String stocktake_id;

    private List<EditText> editTexts = new ArrayList<>();

    public InventoryMatsAdapter(Context context, List<ItemListBean> list, String stocktake_type, String location_id, String stocktake_id) {
        this.context = context;
        this.list = list;
        this.stocktake_type = stocktake_type;
        this.location_id = location_id;
        this.stocktake_id = stocktake_id;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.cy_view_inventory_material_item, null);
            holder = new ViewHolder();
            holder.codeTv = (TextView) view.findViewById(R.id.codeTv);
            holder.describeTv = (TextView) view.findViewById(R.id.describeTv);
            holder.storageNumTv = (TextView) view.findViewById(R.id.storageNumTv);
            holder.tradingNameTv = (TextView) view.findViewById(R.id.tradingNameTv);
            holder.tradingValueTv = (EditText) view.findViewById(R.id.tradingValueTv);
            holder.tradingUnitTv = (TextView) view.findViewById(R.id.tradingUnitTv);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (stocktake_type.equals("1")) {
            holder.storageNumTv.setVisibility(View.VISIBLE);
        } else {
            holder.storageNumTv.setVisibility(View.GONE);
        }

        ItemListBean b = list.get(i);
        if (b.getIs_new().equals("0")) {
            holder.tradingNameTv.setVisibility(View.VISIBLE);
            holder.tradingValueTv.setVisibility(View.VISIBLE);
            holder.codeTv.setText(b.getMat_code());
            holder.describeTv.setText(b.getMat_name());
            holder.storageNumTv.setText("库存" + b.getStock_qty() + b.getUnit_name());
            holder.storageNumTv.setTextColor(context.getResources().getColor(R.color.common_font_item_name));
            holder.tradingNameTv.setText("实盘");
            holder.tradingUnitTv.setText(b.getUnit_name());
            if (TextUtils.isEmpty(b.getActualSendNumber())) {
                holder.tradingValueTv.setText("");
            } else {
                holder.tradingValueTv.setText(b.getActualSendNumber());
            }
            holder.tradingValueTv.setTag(b.getItem_id());
            holder.tradingValueTv.addTextChangedListener(new EditTextWatcher() {
                @Override
                public void onAfterTextChanged(String text) {
                    b.setActualSendNumber(text);
                }
            });

            boolean isRepeat = false;
            for (EditText editText : editTexts) {
                if (String.valueOf(holder.tradingValueTv.getTag()).equals(String.valueOf(editText.getTag()))) {
                    isRepeat = true;
                }
            }
            if (!isRepeat) {
                editTexts.add(holder.tradingValueTv);
            }
            view.setOnClickListener(null);
        } else {
            holder.tradingNameTv.setVisibility(View.GONE);
            holder.tradingValueTv.setVisibility(View.GONE);
            holder.tradingValueTv.addTextChangedListener(null);
            holder.codeTv.setText(b.getMat_code());
            holder.describeTv.setText(b.getMat_name());
            holder.storageNumTv.setText("新增查看详情");
            holder.storageNumTv.setTextColor(context.getResources().getColor(R.color.activity_receive_delivery_overview_font_color));
            holder.tradingUnitTv.setText("新增" + b.getActualSendNumber() + b.getUnit_name());
            view.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Intent intent = new Intent(context, InventoryMatDetailsActivity.class);
                    intent.putExtra(InventoryExtre.ITEMBEAN, b);
                    intent.putExtra(InventoryExtre.LOCATION_ID, String.valueOf(location_id));
                    intent.putExtra(InventoryExtre.STOCKTAKE_ID, String.valueOf(stocktake_id));
                    context.startActivity(intent);
                }
            });
        }

        return view;
    }

    private class ViewHolder {
        private TextView codeTv;
        private TextView describeTv;
        private TextView storageNumTv;
        private TextView tradingNameTv;
        private EditText tradingValueTv;
        private TextView tradingUnitTv;
    }

    public List<EditText> getEditTexts() {
        return editTexts;
    }
}
